package com.dongnao.jack.spring.parse;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

public class ServiceBeanDefinitionParser implements BaseBeanDefinitionParseInterface {

    private String flag = "service";

    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {

        if (support(element.getLocalName())) {

            RootBeanDefinition bf = new RootBeanDefinition(ServiceConfig.class);
            bf.setLazyInit(false);
            String interfaceClass = element.getAttribute("interface");
            String ref = element.getAttribute("ref");
            String protocol = element.getAttribute("protocol");
            bf.getPropertyValues().addPropertyValue("interfaceClass", interfaceClass);
            bf.getPropertyValues().addPropertyValue("ref", ref);
            bf.getPropertyValues().addPropertyValue("protocol", protocol);

            parserContext.getRegistry().registerBeanDefinition(interfaceClass + ref, bf);
            return bf;
        }
        return null;
    }

    @Override
    public boolean support(String elementName) {

        return flag.equals(elementName);
    }

}
